package com.yu.waterstation.system.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.waterstation.common.common.constant.Const;
import com.yu.waterstation.common.common.exception.ServiceException;

import com.yu.waterstation.common.common.util.GeneratorUtil;
import com.yu.waterstation.common.entity.app.Student;
import com.yu.waterstation.common.entity.app.Worker;
import com.yu.waterstation.common.service.RedisService;
import com.yu.waterstation.system.modules.app.mapper.WorkerMapper;
import com.yu.waterstation.system.modules.app.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class WorkerServiceImpl implements WorkerService {

    @Resource
    private WorkerMapper workerMapper;

    @Resource
    private RedisService redisService;

    @Override
    public List<Worker> getAllWorkers()   {
        return workerMapper.selectList(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addWorker(Worker worker) throws Exception {
        if (worker.getPhone() == null)
            throw ServiceException.CONST_phone_can_not_be_empty;
        Integer result = workerMapper.selectCount(new QueryWrapper<Worker>().eq("phone", worker.getPhone()));
        if (result > 0)
            throw ServiceException.CONST_phone_exists;
        worker.setPassword(GeneratorUtil.md5Base64("123456"));
        worker.setStatus(true);
        return workerMapper.insert(worker);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteWorkerBatchIds(List<Integer> workerIdList) throws ServiceException {
        return workerMapper.deleteBatchIds(workerIdList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateWorker(Worker worker) throws Exception {
        if (worker.getPhone() != null) { // 可能要更新手机号时
            Worker workerInDB = workerMapper.selectById(worker.getId());
            if (workerInDB == null)
                throw ServiceException.CONST_user_not_exist;
            if (!worker.getPhone().equals(workerInDB.getPhone())) { // 要更新手机
                Integer count = workerMapper.selectCount(
                        new QueryWrapper<Worker>().eq("phone", worker.getPhone()));
                if (count > 0)
                    throw ServiceException.CONST_phone_exists;
            }
        }
        return workerMapper.updateById(worker);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int resetPassword(Integer workerId)  {

        Worker worker = workerMapper.selectById(workerId);
        worker.setPassword(GeneratorUtil.md5Base64("123456"));
        return workerMapper.updateById(worker);

    }

    @Override
    public int updateWorkerStatus(Integer workerId) {
        Worker worker = workerMapper.selectById(workerId);
        worker.setStatus(!worker.getStatus());
        if(!worker.getStatus()) // 冻结账号
            redisService.hdel(Const.CONST_worker_session_map, workerId.toString()); // 删除学生会话信息
        return workerMapper.updateById(worker);
    }
}
